package com.itfreer.data.metadata.dictionary;

import com.itfreer.data.SysDatabaseHelper;
import com.itfreer.data.dictionary.TableDictionaryBase;
import com.itfreer.data.dictionary.base.DictionaryItem;
import com.itfreer.data.metadata.entity.DictionaryEntity;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 从字典表中处理字典
 */
public class MetadataDictionary extends TableDictionaryBase {

    /**
     * 获取当前索引
     * @return
     */
    @Override
    public int getIndex(){
        return 1000;
    }

    @Override
    public String getText(String dicName, String id) {
        try {
            Dao<DictionaryEntity, String> dao = SysDatabaseHelper.getInstance().getDao(DictionaryEntity.class);
            Map<String, Object> fieldValues = new HashMap<>();
            fieldValues.put("dicName", dicName);
            fieldValues.put("id", id);
            List<DictionaryEntity> items = dao.queryForFieldValues(fieldValues);
            if(items==null || items.size()<=0){
                return null;
            }else{
                return items.get(0).getDisplay();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * '0200', '非林地', '0111','乔木林'
     * @param dicName
     * @return
     */
    @Override
    public String getDictionaryString(String dicName) {
        try {
            Dao<DictionaryEntity, String> dao = SysDatabaseHelper.getInstance().getDao(DictionaryEntity.class);
            Map<String, Object> fieldValues = new HashMap<>();
            fieldValues.put("dicName", dicName);
            List<DictionaryEntity> items = dao.queryForFieldValues(fieldValues);
            if(items==null || items.size()<=0){
                return null;
            }else{
                String text = "";
                for(DictionaryEntity item : items) {
                    text += "'" + item.getId() + "','" + item.getDisplay() + "',";
                }
                text = text.substring(0, text.length()-1);
                return text;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public DictionaryItem getDictionary(String dicName, String id) {
        return null;
    }

    @Override
    public List<DictionaryItem> getChild(String dicName, String id, Boolean getNoValidValue) {
        try {
            Dao<DictionaryEntity, String> dao = SysDatabaseHelper.getInstance().getDao(DictionaryEntity.class);
            Map<String, Object> fieldValues = new HashMap<>();
            fieldValues.put("dicName", dicName);
            fieldValues.put("pid", id);
            if(!getNoValidValue){
                fieldValues.put("valid", true);
            }
            List<DictionaryEntity> items = dao.queryForFieldValues(fieldValues);
            if(items==null || items.size()<=0){
                return null;
            }
            items.sort(new Comparator<DictionaryEntity>() {
                @Override
                public int compare(DictionaryEntity o1, DictionaryEntity o2) {
                    return o1.getOrder().compareTo(o2.getOrder());
                }
            });
            List<DictionaryItem> dicList = new ArrayList<>();
            for(DictionaryEntity item : items){
                DictionaryItem dic = new DictionaryItem();
                dic.setAbbreviation(item.getAbbreviation());
                dic.setCode(item.getCode());
                dic.setDisplay(item.getDisplay());
                dic.setId(item.getId());
                dic.setInfo(item.getInfo());
                dic.setLeaf(item.getLeaf());
                dic.setLevel(item.getLevel());
                dic.setOrder(item.getOrder());
                dic.setPid(item.getPid());
                dic.setUnits(item.getUnits());
                dic.setValid(item.getValid());
                dicList.add(dic);
            }
            return dicList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
